var ROOT='http://api.hongyanche.com';

CONSOLE = {
	urls:{
		home_swiper				:ROOT+"/marketing_admin/swiper_list",
		home_swiper_inactive	:ROOT+"/marketing_admin/swiper_list_inactive",
		home_swiper_add			:ROOT+"/marketing_admin/swiper_add",
		home_swiper_release		:ROOT+"/marketing_admin/swiper_release",
		home_swiper_remove		:ROOT+"/marketing_admin/swiper_remove",
		home_swiper_remove_all	:ROOT+"/marketing_admin/swiper_remove_all",
		home_swiper_range		:ROOT+"/marketing_admin/swiper_range",
		home_swiper_modify		:ROOT+"/marketing_admin/swiper_modify",
	},
	init:function(){
		this.swiper.init();
	},
	swiper:{
		init:function(){
			$(document).on('bc-page-success', '[page_id="home"]', function(event) {
				$(document).on('click', '.swiper-slider', function(event) {
					if($(event.target).closest('.bc-img-remove').length) return;
					DETAIL.put('#swiper-detail',$(this).find('.bc-rank-detail'));$('#swiper-detail').fadeIn()
					event.preventDefault();
				});

				var container1 = $("#home-swiper-img-inactive-container");
				//滑动
				Sortable.create(container1.get(0), {
					animation: 150,
					scrollSensitivity: 100,
					group:"swiper",
				    onSort: function (evt) {
				        var itemEl = evt.item;
				        $(itemEl).trigger('swiper-range');
				    },
				    onAdd:function(evt){
				        var itemEl = evt.item;
						IMAGE.count("#home-swiper-img-inactive-container");
				        $(itemEl).trigger('swiper-inactive');
				    }
				});
				var container2 = $("#home-swiper-img-active-container");
				Sortable.create(container2.get(0), {
					animation: 150,
					scrollSensitivity: 100,
					group:"swiper",
				    onSort: function (evt) {
				        var itemEl = evt.item;
				        $(itemEl).trigger('swiper-range');
				    },
				    onAdd:function(evt){
				        var itemEl = evt.item;
						IMAGE.count("#home-swiper-img-inactive-container");
				        $(itemEl).trigger('swiper-active');
				    }
				});
			});
			$(document).on('bc-rank-success', '#swiper-list-inactive,#swiper-list-active', function(event) {
				var t = $(this);
				IMAGE.count(t);
				t.find(".weui-uploader__file").each(function(index, el) {
					var tt = $(el);
					if(index){
						url = tt.find('url').text();
						if(url) tt.css('background-image',"url("+url+")")
					}
					// console.log($(el));	
				});
				event.preventDefault();
				/* Act on the event */
			});
			$(function(){
				//range
				POST.init(document,{
					url:CONSOLE.urls.home_swiper_range,
					input:function(e){
						var range = []
						// console.log($(e),$(e).parent());
						$(e).parent().find(".bc-img-content.active").each(function(index, el) {
							range.push($(el).find("swiper_id").html());
						})
						range = range.join(",");
						return {range:range};
					},
					loading:function(e){
						return $(e).parents(".weui-uploader").find(".bc-swiper-loading");
					},
					style:'none',
					method:"swiper-range",
				},".bc-img-content")
				//active
				POST.init(document,{
					url:CONSOLE.urls.home_swiper_release,
					input:function(e){
						var swiper_id = $(e).find('swiper_id').html();
						return {swiper_id:swiper_id,release:1};
					},
					style:'none',
					method:"swiper-active",
					success:function(e){
			        	$(e).trigger('swiper-range');
					},
					loading:function(e){
						return $(e).parents(".weui-uploader").find(".bc-swiper-loading");
					},
					error:function(e){
			        	$(e).trigger('swiper-range');
					}
				},".bc-img-content")
				//inactive
				POST.init(document,{
					url:CONSOLE.urls.home_swiper_release,
					input:function(e){
						var swiper_id = $(e).find('swiper_id').html();
						return {swiper_id:swiper_id,release:0};
					},
					style:'none',
					method:"swiper-inactive",
					success:function(e){
			        	$(e).trigger('swiper-range');
					},
					loading:function(e){
						return $(e).parents(".weui-uploader").find(".bc-swiper-loading");
					},
					error:function(e){
			        	$(e).trigger('swiper-range');
					}
				},".bc-img-content")
				//upload-success
				POST.init(document,{
					method:"bc-img-success",
					stop:function(t,event,res){
						if(!res || !res.data || !res.data.url) return true;
						t.append("<url class='hidden'>"+res.data.url+"</url>");
					},
					url:CONSOLE.urls.home_swiper_add,
					input:function(t,event,res){
						return {url:res.data.url}
					},
					style:"none",
					success:function(e,res){
						var e = $(e);
						e.append("<div class='hidden bc-rank-detail'></div>");
						var d = e.children().last();
						var uu = e.find("url").html();
						d.append("<swiper_id class='hidden'>"+res.swiper_id+"</swiper_id>");
						d.append("<url class='hidden'>"+uu+"</url>");
					}
				},'#home-swiper-img-inactive-container .weui-uploader__file');
				POST.init(document,{
					text:"删除",
					url:CONSOLE.urls.home_swiper_remove,
					stop:function(e){
						var img = $(e).parents('.bc-img-content');
						var swiper_id = img.find('swiper_id').html();
						if(!swiper_id) return true
					},
					input:function(e){
						var img = $(e).parents('.bc-img-content');
						var swiper_id = img.find('swiper_id').html();
						return {swiper_id:swiper_id};
					},
					confirm:function(e){
						var img = $(e).parents('.bc-img-content');
						var target = img.find('subject').text() || img.find('swiper_id').text();
						return {target:target}
					},
					success:function(e){
						var img = $(e).parents('.bc-img-content');
						var ee = $(e).closest('.bc-img')
						img.fadeOut(150,function(){
							img.remove();
							IMAGE.count(ee);
						});
					}
				},'.bc-img-remove')
				POST.init(document,{
					text:'删除全部',
					confirm:function(){
						return {target:'临时滑块'}
					},
					url:CONSOLE.urls.home_swiper_remove_all,
					success:function(){
						RANK.reload("#home-swiper-img-inactive-container");
					}
				},"#swiper-remove-all")
				//modify
				POST.init(document,{
					text:"保存为链接",
					url:CONSOLE.urls.home_swiper_modify,
					input:function(){
						var modify = {}
						$("#swiper-detail-link-inputs input").each(function(index, el) {
							var v = $(el).val();
							if(v){
								var n = $(el).attr('name');
								modify[n] = v;
							}
						});
						modify = JSON.stringify(modify);
						var swiper_id = $("#swiper-detail swiper_id").html();
						return {swiper_id:swiper_id,modify:modify};
					},
					confirm:function(){return {}},
					success:function(){
						RANK.reload("#swiper-list-active");
						RANK.reload("#swiper-list-inactive");
					}
				},"#swiper-detail-link-update")
				//modify
				POST.init(document,{
					text:"保存为视频",
					input:function(){
						var modify = {}
						$("#swiper-detail-video-inputs input").each(function(index, el) {
							var v = $(el).val();
							if(v){
								var n = $(el).attr('name');
								modify[n] = v;
							}
						});
						modify = JSON.stringify(modify);
						var swiper_id = $("#swiper-detail swiper_id").html();
						return {swiper_id:swiper_id,modify:modify};
					},
					confirm:function(){return {}},
					success:function(){
						RANK.reload("#swiper-list-active");
						RANK.reload("#swiper-list-inactive");
					},
					url:CONSOLE.urls.home_swiper_modify,
				},"#swiper-detail-video-update")
			})
			$(document).on('bc-detail-success', '#swiper-detail', function(event) {
				var vl = $(this).find("videolink").html();
				if(!vl || !vl.length){
					TAB.select("#swiper-detail-link");
				}else{
					TAB.select("#swiper-detail-video");
				}
			});
		}
	}
}
CONSOLE.init();